﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Freedom.Models;
using Freedom.Models.Entities;
using Freedom.Models.ViewModels;
using Freedom.Models.Interfaces;
using Freedom.Models.Services;
using System.Web.UI;
using IP;

namespace Freedom.Controllers
{
    /// <summary>
    /// 首页控制器
    /// autor: Penn Yao
    /// </summary>
    [HandleError]
    public class HomeController : Controller
    {
        HomeViewModel viewModel;
        IAdInfoService adInfoService;
        IServiceInfoService serviceInfoService;
        ISubscribeInfoService subscribeInfoService;
        Page page;

        public HomeController()
        {
            if (page == null)
                page = new Page(); 
            if (viewModel == null)
                viewModel = new HomeViewModel();
            if (adInfoService == null)
                adInfoService = new AdInfoService();
            if (serviceInfoService == null)
                serviceInfoService = new ServiceInfoService();
            if (subscribeInfoService == null)
                subscribeInfoService = new SubscribeInfoService();
        }

        public ActionResult Index()
        {
            List<tb_AdInfo> adInfoList = null;
            if (adInfoService != null)
                adInfoList = adInfoService.getAllInfo().ToList();

            List<tb_ServiceInfo> serviceInfoList = null;
            if (serviceInfoService != null)
                serviceInfoList = serviceInfoService.getAllInfo().ToList();

            if (viewModel != null)
            {
                viewModel.adInfoList = adInfoList;
                viewModel.serviceInfoList = serviceInfoList;
            }
            return View(viewModel);
        }

        [HttpPost]
        public ActionResult Post([Bind(Include = "Email")]tb_SubscribeInfo subscribeInfo)
        {
            if (subscribeInfoService != null)
            {
                subscribeInfo.Email = subscribeInfo.Email.Equals("请输入您的邮箱") ? null : subscribeInfo.Email;
                subscribeInfo.IP = UserAgent.GetUserAgent().IpAddress;// 问题6
                subscribeInfo.City = Position(subscribeInfo.IP);
                subscribeInfo.CreateTime = DateTime.Now;
                subscribeInfo.UpdateTime = DateTime.Now;
                if (subscribeInfo.Email != null)
                    subscribeInfoService.addInfo(subscribeInfo);
            }

            if (viewModel != null)
                viewModel.subscribeInfo = subscribeInfo;

            return RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 获取地理位置
        /// </summary>
        /// <returns></returns>
        public static string Position(string strIP)
        {
            //strIP = Request.UserHostAddress.ToString().Trim();
            //strIP = "61.146.244.40";//广东省汕头市-汕头市本地上网
            //strIP = "113.96.211.244";//广东省广州市-广州市本地上网
            //strIP = "121.14.162.81";//广东省东莞市-诺基亚N82UC浏览器
            //strIP = "125.88.122.103";//广东省茂名高州市-华为C7600互联星空浏览器
            //ip = "221.238.17.192";//天津市-华为C7600UC浏览器

            string strIPFilePath = @"..\IP\qqwry.dat"; 
            strIPFilePath = System.Web.HttpContext.Current.Server.MapPath(strIPFilePath);
            IPSearch ipSearch = new IPSearch(strIPFilePath);
            IPSearch.IPLocation location = ipSearch.GetIPLocation(strIP);
            return location.country;
        }
    }
}
